package com.tarena.test.luban.demo.rpc.demo01.server;

import com.tarena.test.luban.demo.rpc.demo01.server.service.People;
import com.tarena.test.luban.demo.rpc.demo01.server.service.impl.PeopleImpl;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 利用socketServer监听一个端口 20000
 * 等待客户端 通过socket发送请求
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket server=new ServerSocket(20000);
        //每当有一个请求 服务端要处理一个请求socket对象
        while(true){
            Socket socket = server.accept();
            process(socket);
            System.out.println("接收到了");
            socket.close();
        }
    }
    public static void process(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();
        DataInputStream dis=new DataInputStream(inputStream);
        //反序列化
        String name = dis.readUTF();
        System.out.println("服务端解析:"+name);
        //调用本地方法
        People people=new PeopleImpl();
        String result = people.sayHi(name);
        //返回服务端数据
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dos=new DataOutputStream(outputStream);
        dos.writeUTF(result);
        dos.flush();
    }
}
